package top.hiai.Controller;


import top.hiai.config.BaseController;
import top.hiai.model.Article;

import top.hiai.service.ArcTypeService;
import top.hiai.service.ArticleService;
import top.hiai.service.CommentService;
import top.hiai.service.LinkService;
import top.hiai.service.UserService;


/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top,开发者：黄康权
* @Introduction 前台控制器，对首页文章的遍历
*/

public class IndexController extends BaseController {
	
	int limtCount=15;		//前台每页的数量
	
	ArticleService articleService = new ArticleService();
	ArcTypeService arcTypeService = new ArcTypeService();
	CommentService commentService = new CommentService();
	LinkService linkService = new LinkService();
	UserService userService= new UserService();
	Article article  ;

	
	/**
	 * 前台主界面
	 */
	public void index() {
		//判断用户是否登录了
		
		Integer typeId = getParaToInt("typeId");
		
		String s_name = get("q");

		if(typeId == null){
			setAttr("articleList", articleService.paginate(getParaToInt("p", 1), limtCount,s_name));
		}else{
			setAttr("articleList", articleService.paginateGetTypeId(getParaToInt("p", 1), limtCount,typeId));
		}
		setAttr("arcTypeList", arcTypeService.getArcTypeList());
		//获取热门文章和友情链接
		findHotArticle();
		//传标题
		setAttr("title", "首页");
		render("index.html");
	}
	/**
	 * 文章的详情页面
	 */
	public void article(){
		//获取ID
		Integer aid = getParaToInt();
			
		//在数据库中查找
		article  = articleService.getDetailArticle(aid);
		
		//传标题
		setAttr("title", article.get("name"));
		//文章浏览量加1
		articleService.intArticleView(aid);
		//获取热门文章和友情链接
		findHotArticle();
		//前台传值
		setAttr("article", article);
		
		//获取该文章的评论信息
		setAttr("commentList", commentService.paginateGetArticleId(getParaToInt("p", 1), limtCount,aid));
		//renderJson();
		render("detail.html");	
	}
	/**
	 * 遍历热门文章和友情链接
	 */
	public void findHotArticle(){
		setAttr("articleTuijian", articleService.getArticleTuijian());
		setAttr("articleHot", articleService.getArticleHotList());
		setAttr("linkList", linkService.getArticleHotList());
	}
	/**
	 * 渲染vip购买界面
	 */
	public void vipBuy(){
		setAttr("title", "购买Vip");
		render("vipBuy.html");	
	}
	public void  test(){
		//setAttr("arcTypeList", articleService.getDetailArticle(100));
		//renderJson();
		//render(new MyCaptchaRender(60,22,4,true));
		//Ret ret ; 
		//renderJson(articleService.getCountByUserIdAndArticleId(1,106));		
        //render(new RandomCodeRender("systemRandomCode"));
		
		
		//renderJson(articleService.getArticleTuijian());
		render("test.html");
	}
}
